#include "user_extern.h"

static uint8_t run(void);
static void win_create(void);
static void win_destory(void);
static void update_screen(void);
static void display(void);
static uint8_t button_select(void *arg);
static uint8_t button_enter(void *arg);
static uint8_t button_exit(void *arg);
static uint8_t get_id(void);
static uint8_t get_father_id(void);

static Windows window = {
    .id = WIN_UI_WEATHER,
    .fa_id = WIN_UI_DESKTOP,
    .son_id = {0},
    .name = "weather",
    .win_status = WINDOWS_STATUS_INIT,
    .run = run,
    .update_screen = update_screen,
    .Dselect = button_select,
    .enter = button_enter,
    .exit = button_exit,
};

static void display(void)
{
    char tmp[16] = {0};
    code_to_provin_name(weather.province_code, tmp);
    oled.show_string(32 - strlen(tmp) * 6 / 2, 16, T(tmp), 12, 1);

    code_to_city_name(weather.city_code, tmp);
    oled.show_string(32 - strlen(tmp) * 6 / 2, 32, T(tmp), 12, 1);

    ui.draw_weather(74, 7, weather.weather_code);
    oled.show_string((96 - (strlen(weather.weather_text) * 8 / 2)), 48, T(weather.weather_text), STR_SIZE_16, 1);
}

static void update_screen(void)
{
    oled.refresh_all(1);
}

static void win_destory(void)
{
    window.win_status = WINDOWS_STATUS_INIT;
}

static uint8_t button_select(void *arg)
{
    Dsystem.windows_cur_id = get_father_id();
    window.win_status = WINDOWS_STATUS_DESTORY;
    return 0;
}

static uint8_t button_enter(void *arg)
{
    Dsystem.windows_cur_id = get_father_id();
    window.win_status = WINDOWS_STATUS_DESTORY;
    return 0;
}

static uint8_t button_exit(void *arg)
{
    Dsystem.windows_cur_id = get_father_id();
    window.win_status = WINDOWS_STATUS_DESTORY;
    return 0;
}

/* **************************************************************************************** */

static uint8_t run(void)
{

    switch (window.win_status)
    {
    case WINDOWS_STATUS_NONE:
        return 0;
        break;
    case WINDOWS_STATUS_INIT:
        //win_create();
        display();
        window.win_status = WINDOWS_STATUS_DIS_ONLY;
        break;
    case WINDOWS_STATUS_DIS_ONLY:
        display();
        break;
    case WINDOWS_STATUS_DESTORY:
        win_destory();
        break;
    }
    return window.win_status;
}

static uint8_t get_id(void)
{
    return window.id;
}

static uint8_t get_father_id(void)
{
    return window.fa_id;
}

void win01_weather_init(void)
{
    windows[get_id()] = window;
}
